/* Copyright (c) 2006, Jan Flora <janflora@diku.dk>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the University of Copenhagen nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
  @author Jan Flora <janflora@diku.dk>
*/

#include <LocalTime.h>

module LocalTimeM
{
	provides
	{
		interface LocalTime<time_t> as LocalTime;
	}
	uses
	{
		interface SymbolCounter<time_t> as SymbolCounter;
	}

}
implementation
{	
	async command time_t LocalTime.getTime()
	{
		return call SymbolCounter.getTime();
	}
	
/*	async command uint16_t LocalTime.getHigh16()
	{
		return (call SymbolCounter.getTime())>>16;
	}
	
	async command uint16_t LocalTime.getLow16()
	{
		return (call SymbolCounter.getTime())&0xFFFF;
	}
*/
	async command void LocalTime.reset()
	{
		call SymbolCounter.reset();
	}

	async command bool LocalTime.compare(uint8_t op, time_t timeStamp1, time_t timeStamp2)
	{
		// Check for equality.
		// We use that the equality operations are assigned the smallest values.
		if ((op < TIME_GT) && (timeStamp1 == timeStamp2)) {
			return TRUE;
		}
		
		// Order time stamps.
		if (timeStamp1 > timeStamp2) {
			if ((timeStamp1 - timeStamp2) > 0x7FFFFFFF) {
				// timeStamp2 > timeStamp1
				if (op == TIME_GT || op == TIME_GTE) return FALSE;
				return TRUE;
			} else {
				// timeStamp1 > timeStamp2
				if (op == TIME_GT || op == TIME_GTE) return TRUE;
				return FALSE;
			}
		} else {
			if ((timeStamp2 - timeStamp1) > 0x7FFFFFFF) {
				// timeStamp1 > timeStamp2
				if (op == TIME_GT || op == TIME_GTE) return TRUE;
				return FALSE;
			} else {
				// timeStamp2 > timeStamp1
				if (op == TIME_GT || op == TIME_GTE) return FALSE;
				return TRUE;
			}
		}
	}

	async command time_t LocalTime.timeDiff(time_t before, time_t after)
	{
		time_t diff;
		if (before > after) {
			// We have wrapping.
			diff = ~(before-after)+1;
		} else {
			diff = after-before;
		}
		return diff;
	}
}
